package com.hxx.day7;

import org.junit.Test;

import java.util.*;

/**
 * Map
 */
public class TestDemo6 {
    /**
     * 使用string作为key，因为string是不可变的，所以可以作为key
     */
    @Test
    public void test02(){
        Map map = new HashMap();
        map.put("1001","zhangsan");
        map.put("1002","lisi");
        map.put("1003","wangwu");
        System.out.println(map);
    }
    /**
     * 使用key获取值
     */
    @Test
    public void test03(){
        Map map = new HashMap();
        map.put("1001","zhangsan");
        map.put("1002","lisi");
        map.put("1003","wangwu");
        System.out.println(map);
        // ,使用key获取值
        Object value = map.get("1001");
    }
    /**
     * 循环获取map的值
     */
    @Test
    public void test04(){
        Map map = new HashMap();
        map.put("1001","zhangsan");
        map.put("1002","lisi");
        map.put("1003","wangwu");
        System.out.println(map);
        // 循环获取map的值
        // 方式1：使用keySet()方法
        Set keySet = map.keySet();
        for (Object key : keySet) {
            Object value = map.get(key);
            System.out.println(key+"="+value);
        }
        //获取值
        Collection values = map.values();
        for (Object value : values) {
            System.out.println(value);
        }
        // 方式2：使用entrySet()方法
        Set entrySet = map.entrySet();
        for (Object entry : entrySet) {
            Map.Entry entry1 = (Map.Entry) entry;
            System.out.println(entry1.getKey()+"="+entry1.getValue());
        }
    }

    /**
     * 按照值的降序排序
     * 方式1：使用Comparator接口
     */
    @Test
    public void test05(){
        TreeMap treeMap = new TreeMap((x,y)->((String)y).compareTo((String)x));
        treeMap.put("1001","zhangsan");
        treeMap.put("1002","lisi");
        treeMap.put("1003","wangwu");
        System.out.println(treeMap);
    }

}






























